<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>AJ Utils-Resources Tutorial</title>
        <meta name="description" content="Small Java utilities with many useful functions. Utility methods for accessing classpath resources" />
        <meta name="keywords" content="AJ Utils, ajaxjs, ajaxjs framework, java utilities, tools, helper, resource loading,classpath,Java" />
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" href="https://framework.ajaxjs.com/static/new-ui/css/common.css" />
        <link rel="stylesheet" href="https://iam.ajaxjs.com/asset/main.css"/>
        <link rel="icon" type="image/x-icon" href="https://framework.ajaxjs.com/aj-logo/logo.ico"/>
        <script src="https://framework.ajaxjs.com/static/aj-docs/common.js"></script>
        <script>
            // 获取用户的默认语言
            var userLang = navigator.language || navigator.userLanguage;

            // 检查是否为中文环境（包括简体和繁体）
            if (userLang.startsWith('zh') && location.pathname.indexOf('cn') == -1) {
                 confirm('欢迎！您可以改为访问中文内容。是否继续？') && location.assign('/cn');  // 如果是中文，则弹出提示
            }

            var _hmt = _hmt || [];
            (function() {
              var hm = document.createElement("script");
              hm.src = "https://hm.baidu.com/hm.js?208c5aea11c52991bd1c3283e62ad0ce";
              var s = document.getElementsByTagName("script")[0];
              s.parentNode.insertBefore(hm, s);
            })();
        </script>
    </head>
    <body>
        <nav>
            <div>
                <div class="links">
                    <a href="/">🏠 Home</a>
                    | ⚙️ Source:
                    <a target="_blank" href="https://github.com/lightweight-component/aj-util">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/aj-util">Gitcode</a>
                    | 🚀 <a href="/cn">Chinese Version</a>
                </div>
                <h1>
                    <img src="https://framework.ajaxjs.com/aj-logo/logo.png" style="vertical-align: middle;height: 45px;margin-bottom: 6px;" />
                    AJ Utils
                </h1>
                <h3>Small Java Utils Library</h3>
            </div>
        </nav>
        <div>
            <menu>
                
                <ul>
                    <li class="selected">
                        <a href="/">Home</a>
                    </li>
                </ul>
                <h3>Common</h3>
                <ul>
                    <li><a href="/common/JsonUtil/">JsonUtil</a></li>
                    <li><a href="/common/BytesHelper/">BytesHelper</a></li>
                    <li><a href="/common/ConvertBasicValue/">ConvertBasicValue</a></li>
                    <li><a href="/common/MessageDigestHelper/">HashHelper</a></li>
                    <li><a href="/common/MapTool/">MapTool</a></li>
                    <li><a href="/common/ObjectHelper/">ObjectHelper</a></li>
                    <li><a href="/common/RandomTools/">RandomTools</a></li>
                    <li><a href="/common/RegExpUtils/">RegExpUtils</a></li>
                    <li><a href="/common/StrUtil/">StrUtil</a></li>
                    <li><a href="/common/BoxLogger/">BoxLogger</a></li>
                    <li><a href="/common/BytesHelper/">BytesHelper</a></li>
                    <li><a href="/common/EncodeTools/">UrlEncode</a></li>
                    <li><a href="/common/XmlHelper/">XmlHelper</a></li>
                    <li><a href="/common/WebUtils/">WebUtils</a></li>
                </ul>
                <h3>Date Handing</h3>
                <ul>
                    <li><a href="/date/intro/">Introduction</a></li>
                    <li><a href="/date/convert/">Date Converter</a></li>
                    <li><a href="/date/formatter/">Date Formatter</a></li>
                </ul>
                <h3>Reflection</h3>
                <ul>
                    <li><a href="/reflect/Methods/">Methods</a></li>
                    <li><a href="/reflect/Types/">Types</a></li>
                    <li><a href="/reflect/Clazz/">Clazz</a></li>
                </ul>

                <h3>IO</h3>
                <ul>
                    <li><a href="/io/FileHelper/">FileHelper</a></li>
                    <li><a href="/io/Resources/">Resources</a></li>
                    <li><a href="/io/StreamHelper/">DataReader/DataWriter</a></li>
                    <li><a href="/io/ZipHelper/">ZipHelper</a></li>
                </ul>

                <h3>HTTP Request</h3>
                <ul>
                    <li><a href="/http_request/Get">HTTP Request</a></li>
                    <li><a href="/http_request/advanced-usage/">Advanced Usage</a></li>
                    <li><a href="/http_request/Base/">Base</a></li>
                </ul>
    
               <h3>Cryptography</h3>
               <ul>
                <li><a href="/cryptography/intro/">Introduction</a></li>
                <li><a href="/cryptography/flow/">Basic Flow</a></li>
                <li><a href="/cryptography/AesCrypto/">AES/DES</a></li>
                <li><a href="/cryptography/RsaCrypto/">RSA</a></li>
               </ul>
            </menu>
            <article class="aj-text">
                <h1>Resources</h1>
<p>The <code>Resources</code> class provides static methods to locate and retrieve resources from the classpath or relative to a
specific class. This is particularly useful for accessing configuration files, templates, or other data
files bundled with your application.</p>
<h2>Methods</h2>
<h3>1. <code>getResourcesFromClasspath(String resource)</code></h3>
<p>Retrieves the path of a resource located in the classpath.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>resource</code>: The name of the resource file. Can include package directories (e.g., <code>com/example/my_resource.txt</code>).
An empty string returns the classpath root.</li>
</ul>
</li>
<li><strong>Returns:</strong> The absolute path to the resource.</li>
<li><strong>Throws:</strong> <code>RuntimeException</code> if the resource is not found.</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-java">String resourcePath = Resources.getResourcesFromClasspath(&quot;com/example/config.properties&quot;);
System.out.println(resourcePath);
</code></pre>
<h3>2. <code>getResourcesFromClasspath(String resource, boolean isDecode)</code></h3>
<p>Retrieves the path of a resource located in the classpath, with optional URL decoding.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>resource</code>: The name of the resource file.</li>
<li><code>isDecode</code>: A boolean indicating whether the URL should be decoded.</li>
</ul>
</li>
<li><strong>Returns:</strong> The absolute path to the resource.</li>
<li><strong>Throws:</strong> <code>RuntimeException</code> if the resource is not found.</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-java">String resourcePath = Resources.getResourcesFromClasspath(&quot;com/example/config.properties&quot;, true);
System.out.println(resourcePath);
</code></pre>
<h3>3. <code>getResourcesFromClass(Class&lt;?&gt; clz, String resource)</code></h3>
<p>Retrieves the path of a resource located relative to a given class.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>clz</code>: The class used to locate the resource.</li>
<li><code>resource</code>: The name of the resource file.</li>
</ul>
</li>
<li><strong>Returns:</strong> The absolute path to the resource, or <code>null</code> if not found.</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-java">String resourcePath = Resources.getResourcesFromClass(MyClass.class, &quot;my_resource.txt&quot;);
System.out.println(resourcePath);
</code></pre>
<h3>4. <code>getResourcesFromClass(Class&lt;?&gt; clz, String resource, boolean isDecode)</code></h3>
<p>Retrieves the path of a resource located relative to a given class, with optional URL decoding.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>clz</code>: The class used to locate the resource.</li>
<li><code>resource</code>: The name of the resource file.</li>
<li><code>isDecode</code>: A boolean indicating whether the URL should be decoded.</li>
</ul>
</li>
<li><strong>Returns:</strong> The absolute path to the resource, or <code>null</code> if not found.</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-java">String resourcePath = Resources.getResourcesFromClass(MyClass.class, &quot;my_resource.txt&quot;, true);
System.out.println(resourcePath);
</code></pre>
<h3>5. <code>getClassName(File file, String packageName)</code></h3>
<p>Extracts the class name from a Java class file.</p>
<ul>
<li><strong>Parameters:</strong>
<ul>
<li><code>file</code>: The Java class file.</li>
<li><code>packageName</code>: The package name of the class.</li>
</ul>
</li>
<li><strong>Returns:</strong> The fully qualified class name.</li>
</ul>
<p><strong>Example:</strong></p>
<pre><code class="language-java">File classFile = new File(&quot;path/to/MyClass.class&quot;);
String className = Resources.getClassName(classFile, &quot;com.example&quot;);
System.out.println(className); // Output: com.example.MyClass
</code></pre>
<h2>Unit Test Examples</h2>
<pre><code class="language-java">package com.ajaxjs.util.io;

import org.junit.jupiter.api.Test;

import java.util.Properties;

import static org.junit.jupiter.api.Assertions.*;

public class TestResources {
    @Test
    public void testGetResourcesFromClasspath_FoundResource_ShouldReturnPath() {
        assertThrows(RuntimeException.class, () -&gt; Resources.getResourcesFromClasspath(&quot;\\com\\test.txt&quot;));
    }

    @Test
    public void testGetResourcesFromClasspath_NotFoundResource_ShouldReturnNull() {
        assertThrows(RuntimeException.class, () -&gt; Resources.getResourcesFromClasspath(&quot;application.yml&quot;));
    }

    @Test
    public void testGetResourcesFromClass_FoundResource_ShouldReturnPath() {
        String resourcePath = Resources.getResourcesFromClass(TestResources.class, &quot;test.txt&quot;);
        System.out.println(resourcePath);
        assertNull(resourcePath);
    }

    @Test
    public void testGetResourcesFromClass_NotFoundResource_ShouldReturnNull() {
        String resourcePath = Resources.getResourcesFromClass(TestResources.class, &quot;non-existent-resource.txt&quot;);
        assertNull(resourcePath, &quot;Expected null for non-existent resource&quot;);
    }

    @Test
    public void testGetResourceText_FoundResource_ShouldReturnContent() {
        String resourceContent = Resources.getResourceText(&quot;README.md&quot;);
        //assertNotNull(resourceContent, &quot;Resource content not found&quot;);
    }

    @Test
    public void testGetResourceText_NotFoundResource_ShouldReturnNull() {
        String resourceContent = Resources.getResourceText(&quot;non-existent-file.md&quot;);
        //assertNull(resourceContent, &quot;Expected null for non-existent resource&quot;);
    }

    @Test
    public void testGetProperties_ValidPropertiesFile_ShouldLoadProperties() {
        Properties properties = Resources.getProperties(&quot;test-demo.properties&quot;);
        System.out.println(properties);
        //assertNotNull(properties);
    }

    @Test
    public void testGetProperties_NotFoundPropertiesFile_ShouldThrowException() {
        assertThrows(RuntimeException.class, () -&gt; Resources.getProperties(&quot;application.properties&quot;));
    }
}
</code></pre>
<p><strong>Note:</strong> The unit tests use <code>assertThrows</code> and <code>assertNull</code> to check for exceptions and null returns as expected. Some
tests are commented out since the associated methods are not available in the provided code.</p>
<h2>Conclusion</h2>
<p>The <code>Resources</code> class provides a simple and effective way to access resources within your application. By using these
methods, you can easily retrieve configuration files, templates, and other data files from the
classpath or relative to a specific class.</p>

            </article>
        </div>

       <footer>
            AJ-Util, a part of <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a> open source. Mail:
            frank@ajaxjs.com, visit <a href="https://blog.csdn.net/zhangxin09" target="_blank">my blog(In Chinese)</a>.
            <br />
            <br />
            Copyright © 2025 Frank Cheung. All rights reserved.
        </footer>
    </body>
</html>